const {barrier} = require('./barrier.js');
const SerialPort = require('serialport');
const {SerIP,SerDev,videoDev} = require('./config.js');// 引入配置
const {parser} = require('./parser.js'); // 引入地磅解析函数

if(process.argv.length==2) {
    process.argv[2] = '--help';
}

const args = process.argv;

if(args[2] == '--help') {
    console.log('--help 输出帮助');
    console.log('--pound 3 1200/1600测试磅的波特率');
} else if(args[2] == '--pound') {
    if(args.length<5) {
        console.log('输入磅的编号和波特率');
    } else if(!/\d+/.test(args[3]) || !/\d+/.test(args[4]) ) {
        console.log('磅的编号和波特率应为数字');
    }
    let devNum = args[3];
    SerDev[devNum]['options']['baudRate'] = parseInt(args[4]); 
    let port = new SerialPort(SerDev[devNum]['dev'], SerDev[devNum]['options'],errback);
    port.on('readable', function () {
        let buff = port.read();
        console.log(buff,',',parser.tf0(buff),'吨');
    });
}

function errback(err) {
    if (err) {
      return console.log('Error: ', err.message)
    }
}